package com.huawei.classroom.student.h62;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyHost {
	private String Ip;
	private int Port;
	private String Username;
	private String Password;
	private boolean login;
	private Socket socket;
	private PrintWriter out = null;
	private BufferedReader in = null;
//	这次的真的搞到我了，写了半个下午和半个晚上，Test里越靠前的函数我写在后面，因为我每写到一个都是从变量下面建函数
//	我的函数开头两句基本一样的，写一个指令过去
//	对于那么多try和catch，我懒得声明异常以免到时候Test叫我try，直接由软件给的快捷声明try和catch
	
//	删除文件
	public void Todelete(String Path) {
		if(Toexists(Path)) {
			out.write("delete"+"\t"+Path+"\r\n");
			out.flush();
		}
	}
//	判断文件存在，调用之前写的函数美滋滋
	public boolean Toexists(String Path) {
		if(FileisDirectory(Path) || FileisFile(Path)) {
			return true;
		}
		return false;
	}
//	判断文件大小
	public int Tolength(String Path) {
		out.write("getlength"+"\t"+Path+"\r\n");
		out.flush();
		try {
			return Integer.parseInt(in.readLine());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
//	给文件写东西进去
	public void TowriteByBytes(byte[] content, String Path) {
 		if(!FileisFile(Path)) {
			CreateFile(Path);
		}
		try {
			//我不知道怎么通过out传递byte数组，所以干脆多绕一圈以字符串过去
			String txt = new String(content, "UTF-8");
			out.write("TowriteByBytes"+"\t"+Path+"\t"+txt+"\r\n");
			out.flush();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//	创建文件
	public void CreateFile(String Path) {
		out.write("CreateFile"+"\t"+Path+"\r\n");
		out.flush();
	}
//	判断File是不是文件
	public boolean FileisFile(String FilePath) {
		out.write("isFile"+"\t"+FilePath+"\r\n");
		out.flush();
		String tempreply = null;
		try {
			tempreply = in.readLine();
 		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(tempreply.equals("isFile")) {
			return true;
		}
		else return false;
	}
//	判断File是否目录
	public boolean FileisDirectory(String FilePath) {
		out.write("isDirectory"+"\t"+FilePath+"\r\n");
		out.flush();
		String tempreply = null;
		try {
			tempreply = in.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(tempreply.equals("isDirectory")) {
			return true;
		}
		else return false;
	}
//	列出目录和文件
	public MyRemoteFile[] DisplayFile(String FilePath) {
		out.write("DisplayFile"+"\t"+FilePath+"\r\n");
		out.flush();
		MyRemoteFile[] filearray = null;
		try {
			int count = Integer.parseInt(in.readLine());//得到文件个数（包括目录）
			filearray = new MyRemoteFile[count];
			//Thread.sleep(100);
			for(int i=0; i<count; i++) {
				String t = in.readLine();//服务器命名好然后传回来
				filearray[i] = new MyRemoteFile(this, t);//这里this就是本身这个实例
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return filearray;
	}
//	登录
	public void logining() {
		try {
			this.socket = new Socket(Ip, Port);
			out = new java.io.PrintWriter(socket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out.write("login"+"\t"+Username+"\t"+Password+"\r\n");
			out.flush();
			if(in.readLine().equals("success")) {
				login = true;
			}else if(in.readLine().equals("fail")) {
				login = false;
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//	下面都是set和get函数
	public String getUsername() {
		return Username;
	}

	public String getPassword() {
		return Password;
	}
	
	public void setIp(String Ip) {
		this.Ip = Ip;
	}
	
	public void setPort(int Port) {
		this.Port = Port;
	}
	
	public void setUsername(String Username) {
		this.Username = Username;
	}
	
	public void setPassword(String Password) {
		this.Password = Password;
	}
	
	public boolean getlogin() {
		return login;
	}
}
